<?php

namespace app\business\controller;

use app\models\{Banner, Shoplist, Cate, Goods, Helps};
use think\Db;
use think\Validate;

class System extends Base
{
    public function banner()
    {
        $model = new Banner();
        $shopmodel = new Shoplist();
        $data = $this->request->get();
        $results = $model->order('sorts desc');
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        $results = $results->where('shopid', $this->request->bussiness['id']);
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['shopname'] = $shopmodel->where('id', $result['shopid'])->value('title');
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function banner_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'banner' => 'require',
            ], [
                'title.require' => '活动名称不能为空',
                'banner.require' => '活动图片不能为空',
            ]);
            if (!$validate->check($data)) {
                $this->result([], 1, $validate->getError());
            }
            if (empty($data['type'])) {
                $data['getid'] = 0;
            }
            $data['shopid'] = $this->request->bussiness['id'];
            if ($data['type'] == 1 || $data['type'] == 2) {
                if (empty($data['getid'])) {
                    return json(['code' => 1, 'data' => [], 'msg' => '跳转商品|品类不能为空']);
                }
                //查找商品是否存在
                if ($data['type'] == 1) {
                    $model = new Goods();
                    $goods = $model->where(['id' => $data['getid'], 'shopid' => $data['shopid'], 'isdel' => 1])->find();
                    if (empty($goods)) {
                        return json(['code' => 1, 'data' => [], 'msg' => '商品不存在']);
                    }
                    if ($goods['isSale'] == 2) {
                        return json(['code' => 1, 'data' => [], 'msg' => '商品已下架，请上架后再操作']);
                    }
                }
                //查找商品是否存在
                if ($data['type'] == 1) {
                    $model = new Cate();
                    $cate = $model->where(['id' => $data['getid'], 'shopid' => $data['shopid']])->find();
                    if (empty($cate)) {
                        return json(['code' => 1, 'data' => [], 'msg' => '分类不存在']);
                    }
                }
            }
            $bannermodel = new Banner();
            if (!empty($data['id'])) {
                $res = $bannermodel->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $bannermodel->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Banner();
            $shopmodel = new Shoplist();
            $catemodel = new Cate();
            $goodsmodel = new Goods();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            if ($results) {
                $results['shopname'] = $shopmodel->where('id', $results['shopid'])->value('title');
                if ($results['type'] == 1) {
                    $results['list'] =  $goodsmodel->where(['shopid' => $results['shopid']])->field('id,title')->select();
                }
                if ($results['type'] == 2) {
                    $results['list'] =  $catemodel->where(['shopid' => $results['shopid']])->field('id,name as title')->select();
                }
            }
            return view(null, [
                'row' => $results
            ]);
        }
    }

    public function banner_del()
    {
        $id = trim(input('id'), ',');
        $model = new Banner();
        $res = $model->where(['id' => $id, 'shopid' => $this->request->bussiness['id']])->delete();
        if ($res) {
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function cate()
    {
        $model = new Cate();
        $shopmodel = new Shoplist();
        $data = $this->request->get();
        $results = $model->order(['ishow' => 'asc', 'sorts' => 'desc']);
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        $results = $results->where('shopid', $this->request->bussiness['id']);
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['shopname'] = $shopmodel->where('id', $result['shopid'])->value('title');
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function cate_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'remark' => 'require',

            ], [
                'name.require' => '分类名称不能为空',
                'remark.require' => '分类备注不能为空',
            ]);
            if (!$validate->check($data)) {
                $this->result([], 1, $validate->getError());
            }
            $data['shopid'] = $this->request->bussiness['id'];
            $bannermodel = new Cate();
            if (!empty($data['id'])) {
                $res = $bannermodel->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $bannermodel->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Cate();
            $shopmodel = new Shoplist();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            if ($results) {
                $results['shopname'] = $shopmodel->where('id', $results['shopid'])->value('title');
            }
            return view(null, [
                'row' => $results
            ]);
        }
    }
}
